const Koa = require("koa");
const app = new Koa()
const router = require("koa-router")()
const parser = require("koa-parser");
const cors = require("koa-cors");
app.use(cors());
app.use(parser());

let arr = ["香蕉", "苹果", "鸭梨","西瓜"]

router.get("/fruits", async ctx=> {
    ctx.body = arr
})
router.post("/fruits",async ctx=>{ 
    let fruit = ctx.request.body.fruit;
    arr.push(fruit);
    ctx.body = arr;
})
router.delete("/fruits/:index", async ctx => {
    let index = ctx.params.index;
    arr.splice(index,1);
    ctx.body = arr;
})
router.put("/fruits/:index", async ctx => {
    let index = ctx.params.index;
    let setFruit = ctx.request.body.setFruit;
    arr.splice(index,1,setFruit);
    ctx.body = arr;
})
app.use(router.routes())
app.listen(3000, () => {
    console.log("3000")
})